package com.sphd.restaurantsystem.activitys.funding;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.InvestorItemAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.InvestorInfo;
import com.sphd.restaurantsystem.info.InvestorItemInfo;
import com.sphd.restaurantsystem.presenter.MonthAndDayPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.MonthAndDayView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MonthDetailsActivity extends BaseActivity implements MonthAndDayView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Intent intent;
    Bundle bundle;
    InvestorItemInfo investorItemInfo;
    InvestorInfo investorInfo;
    InvestorItemAdapter invesAdapter;
    MonthAndDayPresenterImpl monthAndDayPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_details);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                bundle.putSerializable(Constant.INVESTORITEM, (Serializable) baseQuickAdapter.getItem(i));
                intent.putExtras(bundle);
                intent.setClass(context, DayDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        investorItemInfo = (InvestorItemInfo) bundle.getSerializable(Constant.INVESTORITEM);
        investorInfo = (InvestorInfo) bundle.getSerializable(Constant.INVESTOR);
        setTitleBar(toolbar, investorInfo.getAccount_name() + investorItemInfo.getmYear());
        monthAndDayPresenter = new MonthAndDayPresenterImpl(this);
        setRefresh(mySwf);
        setRecy(myRecy);
        initAdapter();
        addData();
    }

    private void addData() {
        monthAndDayPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.MONTH + "cgid=" + investorInfo.getAccount_cgid() + "&phone=" +
                investorInfo.getPhone() + "&year=" + investorItemInfo.getmYear());
    }

    private void initAdapter() {
        invesAdapter = new InvestorItemAdapter(R.layout.item_accout_details, new ArrayList<InvestorItemInfo>());
        myRecy.setAdapter(invesAdapter);
    }

    @Override
    public void receRes(List<InvestorItemInfo> investorItemInfos) {
        invesAdapter.setNewData(investorItemInfos);
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
